<?php
class admin extends ci_controller
{

	function __construct(){
		
		parent::__construct();
		$this->load->library('tank_auth');
		$this->load->library('bamer_lib');
		$this->load->model('modul/admin_model');
		$this->load->model('user');
	//	$this->load->model('page');
	//	$this->load->model('notification');
		$this->load->library('security');
		if (!$this->tank_auth->is_logged_in()) redirect('/auth/login/');
		$this->load->helper('bamer_helper');
		$this->lang->load('tank_auth');
		
	}

	function index(){

		//check_permission
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Admin');
		//end

		$data['rows'] = $this->admin_model->list_admin();
		$data['menu'] = 'kelola_pegawai';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/admin/index';
		$this->load->view('layout/inner', $data);
	}

	function show(){
		$this->bamer_lib->check_permission_show($this->tank_auth->get_user_id(), 'Admin');

		$id = $this->uri->segment(4);

		$where = array('id_admin' => $id);
		$where2 = array('id_global' => $id, 'id_user_group' => '2');
		$data['admin'] = $this->user->edit('admin', $where);
		$data['kontak'] = $this->user->edit('kontak', $where2);
		
		
		$data['menu'] = 'kelola_pegawai';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/admin/show';
		$this->load->view('layout/inner', $data);
	}
	

	function add(){
		
		//check_permission
		$row = $this->bamer_lib->check_permission_create($this->tank_auth->get_user_id(), 'Admin');
		//end
		
		$data['menu'] = 'kelola_pegawai';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/admin/add';
		$this->load->view('layout/inner', $data);
	}

	function edit(){
		//check_permission
		$this->bamer_lib->check_permission_update($this->tank_auth->get_user_id(), 'Admin');
		//end

		$id = $this->uri->segment(4);

		$where = array('id_admin' => $id);
		$where2 = array('id_global' => $id, 'id_user_group' => 2);
		$data['admin'] = $this->user->edit('admin', $where);
		$data['kontak'] = $this->user->edit('kontak', $where2);
		
		
		$data['menu'] = 'kelola_pegawai';
		$data['head'] = 'layout/head';
        $data['navbar'] = 'layout/navbar';
		$data['content'] = 'modul/admin/edit';
		$this->load->view('layout/inner', $data);
	
	}

	function create(){

		//check_permission
		$this->bamer_lib->check_permission_create($this->tank_auth->get_user_id(), 'Admin');
		//end

		//validation
		$this->form_validation->set_rules('username_admin', 'Username', 'trim|required|min_length[5]|xss_clean');
		$this->form_validation->set_rules('tgl_lahir_admin', 'Tanggal Lahir', 'trim|required|xss_clean');
		$this->form_validation->set_rules('tgl_diterima_admin', 'Tanggal Diterima', 'trim|required|xss_clean');
		$this->form_validation->set_rules('nama_admin', 'Nama', 'trim|required|min_length[3]|xss_clean');
		$this->form_validation->set_rules('password_admin', 'Password', 'trim|required|min_length[8]|xss_clean');
		$this->form_validation->set_rules('alamat_admin', 'Alamat', 'trim|required|min_length[5]|xss_clean');
		$this->form_validation->set_rules('jenis_kelamin_admin', 'Jenis Kelamin', 'trim|required|min_length[1]|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$data['menu'] = 'kelola_pegawai';
			$data['head'] = 'layout/head';
	        $data['navbar'] = 'layout/navbar';
			$data['content'] = 'modul/admin/add';
			$this->load->view('layout/inner', $data);
		}else{
			$date = get_date_time();

			$config['remove_spaces']  = TRUE;
			$config['upload_path'] = './uploads/foto/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			

			$this->load->library('upload', $config);
			
			$this->upload->do_upload();

			$datetime = strtotime(get_date_time());

			$file = $_FILES["userfile"]["name"];
			$data = array('upload_data' => $this->upload->data());
			$name = $this->input->post('name');

			
			$file = str_replace(' ', '_', $file);

			$data_admin = array(
				'url_foto_admin' => "uploads/foto/".$file,
				'username_admin' => $this->input->post('username_admin'),
				'nama_admin' => $this->input->post('nama_admin'),
				'tgl_lahir_admin' => $this->input->post('tgl_lahir_admin'),
				'tgl_diterima_admin' => $this->input->post('tgl_diterima_admin'),
				'alamat_admin' => $this->input->post('alamat_admin'),
				'jenis_kelamin_admin' => $this->input->post('jenis_kelamin_admin'),
				'password_admin' => MD5($this->input->post('password_admin')),
				);

			$password_original = $this->input->post('password_admin');
			$hasher = new PasswordHash(
				$this->config->item('phpass_hash_strength','tank_auth'),
				$this->config->item('phpass_hash_portable','tank_auth')
				);

			$hashed_password = $hasher->HashPassword($password_original);

			$id_admin = $this->user->insert_get('admin', $data_admin);		
			$data_user = array(
				'username' => $this->input->post('username_admin'),
				'password' => $hashed_password,
				'email' => $this->input->post('kontak')[1],
				'activated' => '1',
				'id_user_group' => '2',
				'created' => date('Y-m-d H:i:s'),
				'id_global' => $id_admin,
				);
			$id_user = $this->user->insert_get('users', $data_user);
			
			$base = base_url();

			
			$url = $this->bamer_lib->url_dir();
			
			$banyak_kontak = $this->input->post('kontak');
			$tipe_kontak = $this->input->post('tipe_kontak');


			$count_banyak_kontak = count($banyak_kontak);

			for($i='1'; $i <= $count_banyak_kontak; $i++){
				if($banyak_kontak[$i]==""){
					$kosong[$i]='1';
				}
			}
			
			$count_kontak_kosong = count($kosong);

			$count = $count_banyak_kontak-$count_kontak_kosong;
			for($i='1'; $i <= $count; $i++){
				$data_kontak[$i] = array(
					'id_user_group' => 2,
					'tipe_kontak' => $tipe_kontak[$i],
					'kontak' => $banyak_kontak[$i],
					'id_global' => $id_admin,
					);

				$this->user->insert('kontak', $data_kontak[$i]);
			}
		
			redirect('modul/admin/index'); 
		}

		
	}

	function update(){
		//check_permission
		$this->bamer_lib->check_permission_update($this->tank_auth->get_user_id(), 'Admin');
		//end

		//validation
	//	$this->form_validation->set_rules('username_admin', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('tgl_lahir_admin', 'Tanggal Lahir', 'trim|required|xss_clean');
		$this->form_validation->set_rules('tgl_diterima_admin', 'Tanggal Diterima', 'trim|required|xss_clean');
		$this->form_validation->set_rules('nama_admin', 'Nama', 'trim|required|min_length[3]|xss_clean');
	//	$this->form_validation->set_rules('password_admin', 'Password', 'trim|required|min_length[6]|xss_clean');
		$this->form_validation->set_rules('alamat_admin', 'Alamat', 'trim|required|min_length[5]|xss_clean');
		$this->form_validation->set_rules('jenis_kelamin_admin', 'Jenis Kelamin', 'trim|required|min_length[1]|xss_clean');

		if ($this->form_validation->run() == FALSE)
		{
			$id = $this->input->post('id_admin');

			$where = array('id_admin' => $id);
			$where2 = array('id_global' => $id, 'id_user_group' => 2);
			$data['admin'] = $this->user->edit('admin', $where);
			$data['kontak'] = $this->user->edit('kontak', $where2);
			
			
			$data['menu'] = 'kelola_pegawai';
			$data['head'] = 'layout/head';
	        $data['navbar'] = 'layout/navbar';
			$data['content'] = 'modul/admin/edit';
			$this->load->view('layout/inner', $data);
		}
		else
		{
			$date = get_date_time();
			$config['remove_spaces']  = TRUE;
			$config['upload_path'] = './uploads/foto/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';

			$this->load->library('upload', $config);

			if ($this->upload->do_upload()) {
				$data = array('upload_data' => $this->upload->data());
				$file = $_FILES["userfile"]["name"];
				$name = $this->input->post('name');


				$base = base_url();

				$path_to_file = './'.$this->input->post('gambar');
				

				unlink($path_to_file);
				$file = str_replace(' ', '_', $file);

				$datetime = strtotime(get_date_time());
				$data_admin = array(
				'url_foto_admin' => "uploads/foto/".$file,
				'username_admin' => $this->input->post('username_admin'),
				'nama_admin' => $this->input->post('nama_admin'),
				'tgl_lahir_admin' => $this->input->post('tgl_lahir_admin'),
				'tgl_diterima_admin' => $this->input->post('tgl_diterima_admin'),
				'alamat_admin' => $this->input->post('alamat_admin'),
				'jenis_kelamin_admin' => $this->input->post('jenis_kelamin_admin'),
			//	'password_admin' => MD5($this->input->post('password_admin')),
				);

				$url = $this->bamer_lib->url_dir();

			}else{
				$data_admin = array(
				'username_admin' => $this->input->post('username_admin'),
				'nama_admin' => $this->input->post('nama_admin'),
				'tgl_lahir_admin' => $this->input->post('tgl_lahir_admin'),
				'tgl_diterima_admin' => $this->input->post('tgl_diterima_admin'),
				'alamat_admin' => $this->input->post('alamat_admin'),
				'jenis_kelamin_admin' => $this->input->post('jenis_kelamin_admin'),
			//	'password_admin' => MD5($this->input->post('password_admin')),
				);
			}

			$id_admin = $this->input->post('id_admin');
			$where_admin = array('id_admin' => $id_admin);
			$this->user->update('admin',$where_admin, $data_admin);

			
			if( !empty($_POST['delete_ids']) and is_array($_POST['delete_ids'])) {
				foreach($this->input->post('delete_ids') as $id_kon) {
					$where = array('id_kontak' => $id_kon);
					$this->user->delete('kontak', $where);
				}
			}

			$where = array('id_global' => $id_admin, 'id_user_group' => 2);
			$rows = $this->user->edit('kontak', $where);
			foreach($rows as $r){
				$where_kontak[$r->id_kontak] = array('id_kontak' => $r->id_kontak,'id_global' => $id_admin, 'id_user_group' => 2);

				$data_kontak[$r->id_kontak] = array(
					'tipe_kontak' => $this->input->post('tipe_kontak')[$r->id_kontak],
					'kontak' => $this->input->post('kontak')[$r->id_kontak],
					);

				$this->user->update('kontak', $where_kontak[$r->id_kontak], $data_kontak[$r->id_kontak]);
			}

			if(!empty($_POST['kontak'])){

				$banyak_kontak = $this->input->post('kontak');
				$tipe_kontak = $this->input->post('tipe_kontak');

				$count_banyak_kontak = count($banyak_kontak);

				for($i='2'; $i <= $count_banyak_kontak; $i++){
					if($banyak_kontak[$i]==""){
						$kosong[$i]='1';
					}
				}
				
				$count_kontak_kosong = count($kosong);

				$count = $count_banyak_kontak-$count_kontak_kosong;
				for($i='2'; $i <= $count; $i++){
					$data_kontak[$i] = array(
						'id_user_group' => 2,
						'tipe_kontak' => $tipe_kontak[$i],
						'kontak' => $banyak_kontak[$i],
						'id_global' => $id_admin,
						);

					$this->user->insert('kontak', $data_kontak[$i]);
				}

			}

			redirect('modul/admin/index');
		}

	}

	function aktif(){
		//check_permission
		$this->bamer_lib->check_permission_delete($this->tank_auth->get_user_id(), 'Admin');
		//end

		$id_admin = $this->uri->segment(4);

		$data_admin = array(
				'status' => 1,
				);
			
		$where_admin = array('id_admin' => $id_admin);
		$this->user->update('admin',$where_admin, $data_admin);

		$data_user = array(
				'banned' => 0, );
			
		$id_user_group = 2;
		$get_id_user = $this->user->get_id_user($id_admin, $id_user_group );

		$where_user = array('id' => $get_id_user);
		$this->user->update('users',$where_user, $data_user);

		redirect('modul/admin/index');
	}

	function nonaktif(){
		//check_permission
		$this->bamer_lib->check_permission_delete($this->tank_auth->get_user_id(), 'Admin');
		//end

		$id_admin = $this->uri->segment(4);

		$data_admin = array(
				'status' => 0,
				);
			
		$where_admin = array('id_admin' => $id_admin);
		$this->user->update('admin',$where_admin, $data_admin);

		$data_user = array(
				'banned' => 1, );
			
		$id_user_group = 2;
		$get_id_user = $this->user->get_id_user($id_admin, $id_user_group );

		$where_user = array('id' => $get_id_user);
		$this->user->update('users',$where_user, $data_user);

		redirect('modul/admin/index');
	}
}
?>